package com.lehe.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 类的相关描述
 *
 * @author 王丹
 * @since 2017-07-13
 */
public class CalculateUtil {

    private static final int DEF_SCALE = 2;//精度

    /** 判断double类型是否相等*/
    public static boolean eq(double double1, double double2) {

        BigDecimal dec1 = new BigDecimal(Double.toString(double1));
        BigDecimal dec2 = new BigDecimal(Double.toString(double2));
        if (dec1.equals(dec2)) {
            return true;
        } else {
            return false;
        }
    }

    public static String add(String str1, String str2) {

        BigDecimal dec1 = new BigDecimal(str1);
        BigDecimal dec2 = new BigDecimal(str2);
        return dec1.add(dec2).setScale(DEF_SCALE).toString();
    }

    public static String sub(String str1, String str2) {

        BigDecimal dec1 = new BigDecimal(str1);
        BigDecimal dec2 = new BigDecimal(str2);
        return dec1.subtract(dec2).setScale(DEF_SCALE).toString();
    }

    public static String mul(String str1, String str2) {

        BigDecimal dec1 = new BigDecimal(str1);
        BigDecimal dec2 = new BigDecimal(str2);
        return dec1.multiply(dec2).setScale(DEF_SCALE).toString();
    }

    public static String div(String str1, String str2) {

        BigDecimal dec1 = new BigDecimal(str1);
        BigDecimal dec2 = new BigDecimal(str2);
        return dec1.divide(dec2, DEF_SCALE, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String div(String str1, String str2, int scale) {

        BigDecimal dec1 = new BigDecimal(str1);
        BigDecimal dec2 = new BigDecimal(str2);
        return dec1.divide(dec2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static double add(double str1, double str2) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.add(dec2).setScale(DEF_SCALE).doubleValue();
    }

    public static double sub(double str1, double str2) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.subtract(dec2).setScale(DEF_SCALE).doubleValue();
    }

    public static double mul(double str1, double str2) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.multiply(dec2).setScale(DEF_SCALE).doubleValue();
    }

    public static String mul2String(double str1, double str2) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.multiply(dec2).setScale(3, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static double div(double str1, double str2) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        //BigDecimal.ROUND_DOWN
        return dec1.divide(dec2, DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double add(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.add(dec2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double sub(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.subtract(dec2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double mul(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.multiply(dec2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double div(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.divide(dec2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static int divtoInt(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.divide(dec2, scale, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static int addtoInt(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.add(dec2).setScale(scale, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static int subtoInt(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        return dec1.subtract(dec2).setScale(scale, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static String format(Double value, String format) {

        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(value);
    }

    /**
     *  BigDecimal除法(四舍五入)
     * @author lizhan
     * @param str1 参数1
     * @param str2 参数2
     * @param scale 保留几位小数
     * @return 返回四舍五入后的结果
     */
    public static double divRoundHalfUp(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        //除法后的结果,四舍五入
        return dec1.divide(dec2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static String divRoundHalfUpToStr(double str1, double str2, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        BigDecimal dec2 = new BigDecimal(Double.toString(str2));
        //除法后的结果,四舍五入
        return dec1.divide(dec2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String doubleToString(double str1, int scale) {

        BigDecimal dec1 = new BigDecimal(Double.toString(str1));
        return dec1.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 日期转毫秒，函数会将字符串表示的日期转换为"yyyy-MM-dd HH:mm:ss"后再获取日期的毫秒。
     * @param date 字符串标识的日期
     * @return 毫秒
     * @author 陈远长
     */
    public static long convertMilliToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long milli = sdf.parse(date).getTime();
            return milli;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期转毫秒。函数会根据调用者指定的格式日期进行毫秒的转换
     * @param date 字符串标识的日期
     * @param format 日期格式
     * @return 毫秒
     * @author 陈远长
     */
    public static long convertMilliToDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            long milli = sdf.parse(date).getTime();
            return milli;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {

        System.out.println(doubleToString(1.1, 0));

        assert 1425864540000L == convertMilliToDate("2015-03-09 09:29:00") : "计算毫秒错误!";
        assert 1425864660000L == convertMilliToDate("2015-03-09 09:29:00") : "计算毫秒错误!";
        //assert System.currentTimeMillis() > convertMilliToDate("2015-03-09 09:29:00") : "计算毫秒错误!";



        assert 1425864540000L == convertMilliToDate("2015-03-09 09:29", "yyyy-MM-dd HH:mm") : "计算毫秒错误!";
        assert 1425864660000L == convertMilliToDate("2015-03-09 09:31", "yyyy-MM-dd HH:mm") : "计算毫秒错误!";
        //System.out.println(System.currentTimeMillis());
    }
}
